package com.sinba.common.news.dao;

import com.sinba.common.dao.BaseDAOImplHibernate;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.news.dto.TopicDTO;
import com.sinba.common.news.entity.Topic;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import org.springframework.util.StringUtils;

import java.util.List;

public class TopicDAO
    extends BaseDAOImplHibernate<Topic>
    implements ITopicDAO
{
    //分页
    public PageDTO findTopicPager( TopicDTO dto, String sord, String sidx )
    {
        final DetachedCriteria dc = DetachedCriteria.forClass( Topic.class );

        if ( dto != null )
        {
            if ( StringUtils.hasText( dto.getTopicName(  ) ) )
            {
                dc.add( Restrictions.like( "topicName",
                                           dto.getTopicName(  ),
                                           MatchMode.ANYWHERE ) );
            }

            if ( StringUtils.hasText( dto.getDescription(  ) ) )
            {
                dc.add( Restrictions.like( "description",
                                           dto.getDescription(  ),
                                           MatchMode.ANYWHERE ) );
            }

            if ( StringUtils.hasText( dto.getTopicType(  ) ) )
            {
                dc.add( Restrictions.like( "topicType",
                                           dto.getTopicType(  ),
                                           MatchMode.ANYWHERE ) );
            }
        }

        //排序
        if ( ( sord != null ) && ! sord.equals( "" ) && ( sidx != null ) && ! sidx.equals( "" ) )
        {
            if ( sord.equals( "desc" ) )
            {
                dc.addOrder( Order.desc( sidx ) );
            } else{
                dc.addOrder( Order.asc( sidx ) );
            }
        }

        return super.findPageByCriteria( dc,
                                         dto.getStart(  ),
                                         dto.getLimit(  ) );
    }
    ;

    /**
     * ��ѯȫ��
     * @param dto
     * @param sord
     * @param sidx
     * @return
     */
    public List<Topic> findTopicAll( TopicDTO dto, String sord, String sidx )
    {
        final DetachedCriteria dc = DetachedCriteria.forClass( Topic.class );

        if ( dto != null )
        {
            if ( StringUtils.hasText( dto.getTopicName(  ) ) )
            {
                dc.add( Restrictions.like( "topicName",
                                           dto.getTopicName(  ),
                                           MatchMode.ANYWHERE ) );
            }

            if ( StringUtils.hasText( dto.getDescription(  ) ) )
            {
                dc.add( Restrictions.like( "description",
                                           dto.getDescription(  ),
                                           MatchMode.ANYWHERE ) );
            }

            if ( StringUtils.hasText( dto.getTopicType(  ) ) )
            {
                dc.add( Restrictions.eq( "topicType",
                                         dto.getTopicType(  ) ) );
            }
        }

        //����
        if ( ( sord != null ) && ! sord.equals( "" ) && ( sidx != null ) && ! sidx.equals( "" ) )
        {
            if ( sord.equals( "desc" ) )
            {
                dc.addOrder( Order.desc( sidx ) );
            } else{
                dc.addOrder( Order.asc( sidx ) );
            }
        }

        return super.getHibernateTemplate(  ).findByCriteria( dc );
    }
}
